<?php
namespace Api\Controller;

use Org\Util\String;
use Common\Lib\Util\Validate;
use Admin\Model\UserModel;

class IdentifyController extends ApiController {

    function __construct() {
        parent::__construct();
    }
    
    //获取随机验证码
    public function index(){
    	$len = I('len') ? I('len') : 4;
    	return $this->response('Success',200,String::randString($len));
    }
    
    //获取短信验证码
    public function getEmsCode($mobile='', $len=6, $type=0){
    	$types = array('sendRegisterCode','sendPwdResetCode','sendLoginCode');
    	//验证码类型 0 用户注册 1 密码修改  2验证码登录
    	$type = $type ? $type : (I('type') ? I('type') : 0);
    	$len = $len ? $len : I('len');
    	$mobile = $mobile ? $mobile : I('mobile');
    	if (!Validate::isMobile($mobile)) {
            return $this->response('无效的手机号码！',1);
    	}
	    $userModel = new UserModel();
	    $isExist = $userModel->checkUserExistByMobile($mobile);
    	if ($type == 0) {
            if ($isExist > 0) {
                return $this->response('该手机号码已存在！',1);
            }
    	}
        if ($type == 1 || $type == 2) {
            if ($isExist == 0) {
                return $this->response('该手机号码不存在！',1);
            }
    	}
    	$code = String::randString($len,1);
    	$rs = $types[$type]($mobile,$code);
    	$status = $rs  == 0 ? 200 : 0;
    	return $this->response($rs,$status,$code);
    }
}